<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2018/1/30
 * Time: 21:47
 */

namespace controller;


class Tools
{
    public function img($img, $width = 300, $height = 0)
    {
        //原始大图地址，缩略图宽度，高度，缩略图地址
        if (strpos($img, 'http') === false) {
            $img = '/yjdata/www/www' . $img;
        }

        $imgage = getimagesize($img); //得到原始大图片
        switch ($imgage[2]) { // 图像类型判断
            case 1:
                header("Content-Type: image/gif");
                $im = imagecreatefromgif($img);
                break;
            case 2:
                header('Content-type: image/jpg');
                $im = imagecreatefromjpeg($img);
                break;
            case 3:
                header("Content-Type: image/png");
                $im = imagecreatefrompng($img);
                break;
            default:
                header('Content-type: image/jpg');
                $im = imagecreatefromjpeg($img);
                break;
        }
        $src_W = $imgage[0]; //获取大图片宽度
        $src_H = $imgage[1]; //获取大图片高度

        if ($height == 0) {
            $height = ($width / $src_W) * $src_H;
        }
        $tn = imagecreatetruecolor($width, $height); //创建缩略图
        imagecopyresampled($tn, $im, 0, 0, 0, 0, $width, $height, $src_W, $src_H); //复制图像并改变大小
        imagejpeg($tn, null, 50); //输出图像
    }
}